package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Item;

@Repository(value = "itemDao")
@Transactional(readOnly = false)
public class ItemDaoImpl implements ItemDao {
	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	
	public List<Item> getListItem(){
		TypedQuery<Item> q = em.createNamedQuery("Item.findAll",Item.class);
    	return q.getResultList();
	}	
	public List<Item> getListItemByPackageId(Integer packageId){
		TypedQuery<Item> q = em.createNamedQuery("Item.findByPackageId",Item.class);
    	q.setParameter("packageId",packageId);
    	return q.getResultList();
	}
	public List<Item> getListItemByPackageIdByState(Integer packageId,short state){
		TypedQuery<Item> q = em.createNamedQuery("Item.findByPackageIdByState",Item.class);
    	q.setParameter("packageId",packageId);
    	q.setParameter("state",state);
    	return q.getResultList();
	}	
	public List<Item> getListItemByItemId(Integer itemId){
		TypedQuery<Item> q = em.createNamedQuery("Item.findByItemId",Item.class);
    	q.setParameter("itemId",itemId);
    	return q.getResultList();
	}
	public List<Item> getListItemByStockId(Integer stockId){
		TypedQuery<Item> q = em.createNamedQuery("Item.findByStockId",Item.class);
    	q.setParameter("stockId",stockId);
    	return q.getResultList();
	}
	public List<Item> getListItemByStockIdByProviderId(Integer stockId,Integer providerId){
		TypedQuery<Item> q = em.createNamedQuery("Item.findByStockIdByProviderId",Item.class);
    	q.setParameter("stockId",stockId);
    	q.setParameter("providerId",providerId);
    	return q.getResultList();
	}
	public List<Item> getListItemByPharmacyId(Integer pharmacyId){
		TypedQuery<Item> q = em.createNamedQuery("Item.findByPharmacyId",Item.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	return q.getResultList();
	}
	public List<Item> getListItemByPharmacyIdByStockId(Integer pharmacyId,Integer stockId){
		TypedQuery<Item> q = em.createNamedQuery("Item.findByPharmacyIdByStockId",Item.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("stockId",stockId);
    	return q.getResultList();
	}
	public List<Item> getListItemByPharmacyIdByStockIdByPackageId(Integer pharmacyId,Integer stockId,Integer packageId){
		TypedQuery<Item> q = em.createNamedQuery("Item.findByPharmacyIdByStockIdByPackageId",Item.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("stockId",stockId);
    	q.setParameter("packageId",packageId);
    	return q.getResultList();
	}	
	public List<Item> getListItemByPharmacyIdByStockIdByPackageId(Integer stockId,Integer packageId){
		TypedQuery<Item> q = em.createNamedQuery("Item.findByStockIdByPackageId",Item.class);
    	q.setParameter("stockId",stockId);
    	q.setParameter("packageId",packageId);
    	return q.getResultList();
	}
	public List<Item> getListItemByPharmacyIdByStockIdByPackageIdByState(Integer stockId,Integer packageId,short state){
		TypedQuery<Item> q = em.createNamedQuery("Item.findByStockIdByPackageIdByState",Item.class);
    	q.setParameter("stockId",stockId);
    	q.setParameter("packageId",packageId);
    	q.setParameter("state",state);// 
    	return q.getResultList();
	}
	public Item findItemByPackageIdByItemId(Integer packageId,Integer itemId){
		Item res = null;
    	Query  q = em.createNamedQuery("Item.findByPackageIdByItemId");
    	q.setParameter("packageId",packageId);
    	q.setParameter("itemId",itemId);
    	if(q.getResultList().size()==1)
    		res=(Item)q.getSingleResult();
    	return res;
	}				
	
	public Integer findByItemIdMax(Integer packageId){
		Integer res=0;
		try {
			Query  q = em.createNamedQuery("Item.findByItemIdMax");
	    	q.setParameter("packageId",packageId);
	    	if(q.getResultList().size()==1)
	    		res=(Integer)q.getSingleResult();
			} catch (Exception e) {
				res = 1;
			}
		return res;
	}
	
    public void update(Item item){
    	em.merge(item);
    }
	public void add(Item item){
		em.persist(item);
	}
	
	
	public List<Item> getListFind(){
		TypedQuery<Item> q = em.createNamedQuery("Item.find",Item.class);
    /*	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("stockId",stockId);	*/
    	return q.getResultList();
	}
}
